clear
set mem 1g
set more off
prog drop _all
capture log close

pause on


*global dir_data_out     "../model/out_datasmm_new" 


capture program drop getGroupMean
program getGroupMean

	args yvar xvar
	
	preserve
	
		keep if `yvar' != . & `xvar' != . 
				
		local ly : variable label `yvar'
		local lx : variable label `xvar'
		
		gcollapse (mean) `yvar', by(`xvar') 
					
		** output 
*		outsheet `xvar' `yvar' using $dir_data_out/mean_`yvar'_BY_`xvar'.txt, nonames noquote nolabel replace
		outsheet `xvar' `yvar' using out_datasmm_new/mean_`yvar'_BY_`xvar'.txt, nonames noquote nolabel replace
		
		
			
	restore
	
end

capture program drop get_seGroupMean
program get_seGroupMean

	args yvar xvar
	
	preserve
	
		keep if `yvar' != . & `xvar' != . 
		
		sum `yvar'
		
		if ( `r(max)' == 1 & `r(min)' == 0) {
		
			** se = sqrt(p(1-p) ) / sqrt(n), the difference lies when calculating sample sd compared to the normal case  
			gcollapse (sebinomial) se = `yvar' (mean) m = `yvar' (count) n = `yvar', by(`xvar') 
			
			local z_min = 1.96 
			local z_max = 1.96 

		}
		else {
			** se =  sqrt( sum(yvar-mean)^2 / (n-1) ) / sqrt(n)
			gcollapse (semean) se = `yvar' (mean) m = `yvar', by(`xvar') 
			
			local z_min = 1.96 
			local z_max = 1.96 
		
		}
		
		** output
*		outsheet `xvar' se using $dir_data_out/semean_`yvar'_BY_`xvar'.txt, nonames noquote nolabel replace
		outsheet `xvar' se using out_datasmm_new/semean_`yvar'_BY_`xvar'.txt, nonames noquote nolabel replace
		
				
	restore
	
end


******************************************************************
******************************************************************

use NLSY97_PublicData_FinalSample, clear

gen dq0 = dq if addiction == 0
label var dq0 "Initiate Smoking" 

gen logearnings_de0 = log(earnings) if enroll == 0 & earnings > 0 
label var logearnings_de0 "Log Earnings"


foreach yvar of varlist addiction dq0 hgc logearnings_de0 {
getGroupMean `yvar' age
}


foreach yvar of varlist addiction dq0 hgc logearnings_de0 {
get_seGroupMean `yvar' age
}

capture log close 

